package com.yuncheng.spcyApi.selectXzxkApi.service;

import com.yuncheng.spcyApi.constant.sys.SysConfigConstant;
import com.yuncheng.spcyApi.entity.SpcySqsxBasic;
import com.yuncheng.spcyApi.entity.SpcySqsxBczgjl;
import com.yuncheng.spcyApi.entity.SpcySqsxBqbzjl;
import com.yuncheng.spcyApi.mapper.XzXkMapper;
import com.yuncheng.spcyApi.selectXzxkApi.vo.XzspXzxdrJbxxVo;
import com.yuncheng.spcyApi.service.ISysConfigService;
import com.yuncheng.spcyApi.utils.SmsUtils;
import com.yuncheng.spcyApi.vo.sys.SysConfigSmsVo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Optional;

/**
 * @author hua
 */
@Component(value = "xzspXzxkService")
public class XzspXzxkService {

    private static final Logger log = LoggerFactory.getLogger(XzspXzxkService.class);

    @Resource
    @Lazy
    private XzXkMapper xzXkMapper;

    @Resource
    private SmsUtils smsUtils;

    @Resource
    private ISysConfigService sysConfigService;

    private final boolean isSendSms = false;

    /**
     * 流程短信是否开启
     * @return
     */
    private SysConfigSmsVo verifyQySmsIsEnable(){
        SysConfigSmsVo smsVo = new SysConfigSmsVo();
        String smsFlag = Optional.ofNullable(sysConfigService.getSysConfigByCode(SysConfigConstant.QY_SMS_FLAG))
                .orElseGet(()->"0");
        if (StringUtils.isBlank(smsFlag) || smsFlag.equals("0")){
            smsVo.setSendFlag(false);

            /** 默认发送短信号码 */
            String defaultPhone = Optional.ofNullable(sysConfigService.getSysConfigByCode(SysConfigConstant.MRFSHM))
                    .orElseGet(()->"18278813781");
            smsVo.setDefaultPhone(defaultPhone);
        }else {
            smsVo.setSendFlag(true);
            smsVo.setDefaultPhone("");
        }

        return smsVo;
    }

    /**
     * 退回政务-给企业联系人发生短信
     */
    public void thzwSendSmsToQyLxr(SpcySqsxBasic sqsx){
        if (sqsx == null || StringUtils.isBlank(sqsx.getFthzwdxnr())){
            return;
        }

        this.sendQySms(this.getQyPhone(sqsx), sqsx.getFthzwdxnr());
    }

    /**
     * 给企业发生短信-补齐补正
     */
    public void bqbzSendSmsToQyLxr(SpcySqsxBqbzjl bqbzyj, SpcySqsxBasic sqsx){
        if (bqbzyj == null || StringUtils.isBlank(bqbzyj.getFdxnr())){
            return;
        }

        this.sendQySms(this.getQyPhone(sqsx), bqbzyj.getFdxnr());
    }

    /**
     * 给企业发生短信-整改通知书
     */
    public void zgtzSendSmsToQyLxr(SpcySqsxBczgjl bczgjl, SpcySqsxBasic sqsx){
        if (bczgjl == null || StringUtils.isBlank(bczgjl.getFdxnr())){
            return;
        }

        this.sendQySms(this.getQyPhone(sqsx), bczgjl.getFdxnr());
    }

    /** 获取电话 */
    private String getQyPhone(SpcySqsxBasic sqsx){
        String phone = "";

        if (sqsx != null && StringUtils.isNotBlank(sqsx.getFqylxrdh())){
            phone = sqsx.getFqylxrdh();

            return phone;
        }

        XzspXzxdrJbxxVo xzspXzxdrJbxxVo = null;
        try {
            xzspXzxdrJbxxVo = xzXkMapper.selectQylxrxgxxBySqsx(sqsx.getFsxsqjbxxid());
            if (xzspXzxdrJbxxVo == null){
                return phone;
            }
        }catch (Exception e){
            log.error("查询企业政务库错误！");
            xzspXzxdrJbxxVo = null;
        }

        if (xzspXzxdrJbxxVo != null) {
            if (StringUtils.isNotBlank(xzspXzxdrJbxxVo.getLxdh())) {
                phone = xzspXzxdrJbxxVo.getLxdh();

            } else if (StringUtils.isNotBlank(xzspXzxdrJbxxVo.getLxrsj())) {
                phone = xzspXzxdrJbxxVo.getLxrsj();
            }
        }

        return phone;
    }

    /** 发生短信 */
    private void sendQySms(String phone, String msg){
        if (StringUtils.isBlank(phone)){
            log.error("发生企业短信失败！");
        }

        /** 短信配置 */
        SysConfigSmsVo smsVo = verifyQySmsIsEnable();
        if (!smsVo.isSendFlag()){
            phone = smsVo.getDefaultPhone();
        }

        try {
            smsUtils.sendSmsZdy(phone, msg);

        } catch (NoSuchAlgorithmException e) {
            log.error("给企业联系人发生短信！[NoSuchAlgorithmException]-{}",e.getMessage());
        } catch (IOException e) {
            log.error("给企业联系人发生短信！[IOException]-{}",e.getMessage());
        } catch (InvalidKeySpecException e) {
            log.error("给企业联系人发生短信！[InvalidKeySpecException]-{}",e.getMessage());
        }
    }
}
